<template>
	<view class="login-box">

		<view class="login-content">
			<image class="logo" :src="logo"></image>
			<text class="title">中盈联盟商家</text>
			<view class="content">
				<u--input placeholder="请输入手机号" :maxlength="11" type="number" :height="40" :focus="false"
					v-model="userInfo.userName" border="none"></u--input>
				<u-input placeholder="请输入密码" :maxlength="50" :height="40" :focus="false" v-model="userInfo.password"
					border="none" type="password" v-if="type=='password'">
					<template slot="suffix">
						<view @click="showPwd">
							<u-icon color="#999" size="24" name="eye-off"></u-icon>
						</view>
					</template>
				</u-input>
				<u-input placeholder="请输入密码" :maxlength="50" :height="40" :focus="false" v-model="userInfo.password"
					border="none" type="text" v-if="type=='text'">
					<template slot="suffix">
						<view @click="showPwd">
							<u-icon color="#999" size="24" name="eye"></u-icon>
						</view>
					</template>
				</u-input>
				<view class="code-box">
					<u-input placeholder="请输入验证码" :maxlength="50" :height="40" :focus="false" v-model="userInfo.smsCode"
						border="none" type="text">
						<template slot="suffix">
							<u-code :seconds="seconds" ref="uCode" @change="codeChange"></u-code>
							<u-button @tap="getCode" :text="tips" type="success" size="mini"></u-button>
						</template>
					</u-input>

				</view>
				<u-input placeholder="请输入推荐人手机号" :maxlength="50" :height="40" :focus="false"
					v-model="promoterUserPhone" border="none" type="number">

				</u-input>

			</view>
			<view :class="screenWidth < 375 ? 'big btn-box' : 'btn-box'" style="margin-top: 30rpx;">
				<view>
					<u-checkbox-group size="16" v-model="checked">
						<u-checkbox name="1" inactiveColor="#ccc" activeColor="#00A8FF"></u-checkbox>
					</u-checkbox-group>
				</view>
				<text @click="myFun">我已阅读并同意接受</text>
				<text class="act-color" @click="toPage('1')">《注册协议》</text>

			</view>
			<u-toast ref="uToast"></u-toast>
			<view class="sub">

				<u-button type="primary" @tap="submit">注 册</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getStoreData,
		setStoreData,
		httpPost,
		httpGet
	} from "@/utils/http.js";
	const app = getApp();
	const globalData = app.globalData;
	const wxApi = globalData.wxApi;
	const logo = require(`@/img/logo.png`);
	export default {
		data() {
			return {
				show: false,
				isiPhoneX: false,
				screenWidth: 375,
				token: "",
				logo,
				type: "password",
				checked: [],
				userInfo: {
					userName: "",
					password: "",
					smsCode: "",
					promoterUserId: "",
					openId:"",
					publicOpenId:""
				},
				promoterUserPhone:"",
				showPassword: false,
				tips: "获取验证码",
				seconds: 60
			};
		},
		onShow() {

			const _this = this;
			uni.getSystemInfo({
				success(res) {
					_this.screenWidth = res.screenWidth;
					setStoreData("screenWidth", res.screenWidth);
					setStoreData("screenHeight", res.screenHeight);
					if (res.model.indexOf("iPhone") > -1) {
						const num = res.model.charAt(7);
						if (!isNaN(num) && num >= 4 && num <= 8) {
							_this.isiPhoneX = false;
						} else {
							_this.isiPhoneX = true;
						}
					} else {
						_this.isiPhoneX = false;
					}
					setStoreData("isiPhoneX", _this.isiPhoneX);
				},
			});

		},
		onLoad() {

		},
		methods: {
			showPwd() {
				if (this.type === "password") {
					this.type = "text";
				} else {
					this.type = "password";
				}
			},


			toPage(v) {
				if (v === "1") {
					uni.navigateTo({
						url: "/pages/user/agreement",
					});
				} else if (v === "2") {
					uni.navigateTo({
						url: "/pages/user/store-agreement",
					});
				} else if (v === "3") {
					uni.navigateTo({
						url: "/pages/user/update-pwd",
					});
				}
			},
			myFun() {
				if (this.checked && this.checked.length) {
					this.checked = [];
				} else {
					this.checked = ["1"];
				}
			},
			async submit() {
				let  userId= await this.getPromoterUser(this.promoterUserPhone);
				this.userInfo.promoterUserId=userId;
				console.log(this.userInfo)
				let reSult=await this.getMiniOpenid();
				let reSultTwo=await this.getPublicOpenId();
				if (!this.userInfo.userName) {
					uni.$u.toast('请输入手机号');
					return
				} else if (!this.userInfo.password) {
					uni.$u.toast('请输入密码');
					return
				} else if (!this.userInfo.smsCode) {
					uni.$u.toast('请输入验证码');
					return
				} else if (!this.promoterUserPhone) {
					uni.$u.toast('请输入推荐人手机号');
					return
				}else if (this.checked.length==0) {
					uni.$u.toast('请勾选注册协议');
					return
				}
				
				httpPost("/api/user/register", this.userInfo)
					.then((res) => {
						console.log(res)
						if(res.success){
							uni.$u.toast('注册成功');
							uni.reLaunch({
								url: '/pages/index/index'
							});
						}
                        
					})
					.catch((e) => {

					});

			},
             getPromoterUser(phone){
				 let that=this;
				 return new Promise((resolve, reject)=>{
					 httpGet(`/api/user/info/${phone}`)
					 	.then((res) => {
					 		console.log(res)
							if(res.data){
								resolve(res.data.id);
							}
					 	    
					 	})
					 	.catch((e) => {
					 
					 	});
				 })
			 },
			getMiniOpenid(){
				let that=this;
				return new Promise((resolve, reject)=>{
					uni.login({
						success:res=>{
							console.log('登录',res)
							httpPost("/api/user/register", this.userInfo)
								.then((res) => {
									
									if(res.success){
									
									}
							        
								})
								.catch((e) => {
							
								});
						}
					});
				})
			},
			getPublicOpenId(){
				let that=this;
				return new Promise((resolve, reject)=>{
					uni.login({
						success:res=>{
							
						}
					});
				})
			},
			getCode() {
				let that = this;
				if (!this.userInfo.userName) {
					uni.$u.toast('请输入手机号');
					return
				}
				if (that.$refs.uCode.canGetCode) {

					uni.showLoading({
						title: '正在获取验证码'
					})

					httpGet(`/api/sms/send/register/${this.userInfo.userName}`)
						.then((res) => {
							console.log(res)
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							uni.$u.toast('验证码已发送');
							// 通知验证码组件内部开始倒计时
							that.$refs.uCode.start();
						})
						.catch((e) => {

						});

				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			codeChange(text) {
				this.tips = text;
			},
		},
	};
</script>
<style lang="scss">
	@import "./index.scss";

	.code-box {
		margin-bottom: 30rpx;
	}
</style>